package legumes.shared.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Id;

import com.googlecode.objectify.annotation.Serialized;

public class Menu implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Id private long date = new Date().getTime();
	@Serialized private List<MenuItem> menuWholeItems = new ArrayList<MenuItem>();
	@Serialized private List<MenuItem> menuHalfItems = new ArrayList<MenuItem>();
	private float wholePrice = 14f;
	private float halfPrice = 7f;
	
	public long getDate() {
		return date;
	}

	public void setDate(long date_) {
		date = date_;
	}

	/**
	 * @return the wholePrice
	 */
	public float getWholePrice() {
		return wholePrice;
	}

	/**
	 * @param wholePrice
	 *            the wholePrice to set
	 */
	public void setWholePrice(float wholePrice) {
		this.wholePrice = wholePrice;
	}

	/**
	 * @return the halfPrice
	 */
	public float getHalfPrice() {
		return halfPrice;
	}

	/**
	 * @param halfPrice
	 *            the halfPrice to set
	 */
	public void setHalfPrice(float halfPrice) {
		this.halfPrice = halfPrice;
	}

	public List<MenuItem> getMenuWholeItems() {
		return menuWholeItems;
	}

	public void setMenuWholeItems(List<MenuItem> orderItems) {
		this.menuWholeItems = orderItems;
	}

	public List<MenuItem> getMenuHalfItems() {
		return menuHalfItems;
	}

	public void setMenuHalfItems(List<MenuItem> orderItems) {
		this.menuHalfItems = orderItems;
	}

	@Override
	public boolean equals(Object obj) {
		return getDate() == ((Menu) obj).getDate();
	}

	public void addMenuWholeItem(MenuItem lc_) {
		menuWholeItems.add(lc_);
	}
	
	public void addMenuHalfItem(MenuItem lc_) {
		menuHalfItems.add(lc_);
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("[Menu id=").append(getDate()).append(" items=");
		for (MenuItem lc : getMenuWholeItems()) {
			sb.append("\t").append(lc.toString()).append("\n");
		}

		return sb.toString();
	}

	public MenuItem findWholeMenuItemFor(String productName_) {
		for (MenuItem mi : getMenuWholeItems()) {
			if (mi.getArticleName().equals(productName_)) {
				return mi;
			}
		}
		return null;
	}
	
	public MenuItem findHalfMenuItemFor(String productName_) {
		for (MenuItem mi : getMenuHalfItems()) {
			if (mi.getArticleName().equals(productName_)) {
				return mi;
			}
		}
		return null;
	}

	public Cost computeRealPrice() {
		Cost cost = new Cost();
		cost.plus(computeMenuWholePrice() );
		cost.plus(computeMenuHalfPrice() );
		return cost;
	}

	public Cost computeMenuHalfPrice() {
		Cost cost = new Cost();
		for (MenuItem mi : getMenuHalfItems()) {
			cost.plus(mi.computeRealPrice());
		}
		return cost;
	}

	public Cost computeMenuWholePrice() {
		Cost cost = new Cost();
		for (MenuItem mi : getMenuWholeItems()) {
			cost.plus(mi.computeRealPrice());
		}
		return cost;
	}
	
	public void clear() {
		menuWholeItems.clear();
		menuHalfItems.clear();
		setHalfPrice(0);
		setWholePrice(0);
	}

	public void setMenu(Menu menu_) {
		clear();
		setHalfPrice(menu_.getHalfPrice());
		setWholePrice(menu_.getWholePrice());
	}
}